<template>
	<om-formsub-outer :index="index" :customAddFunc="true" :customDelFunc="true"  @save="save" @delete="del">
		<uni-card :titleUnderline="false" :is-full="true" style="border: none;border-radius: 1px;" titleBgColor="#eee" :title="'明细(' + order + ')'">
			<uni-list>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="代码" required></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.orderNum }}</view>
						<view v-else class="tui-right tui-col-7" @click="orderNumFunc">
							<view>{{ formdata.orderNum || '请选择' }}</view>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="工作项名称"></om-label></view>
						<view class="tui-col-7">{{ formdata.workName }}</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="交付物名称"></om-label></view>
						<view class="tui-col-7">{{ formdata.deliveryName }}</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="原计划完成月份"></om-label></view>
						<view class="tui-col-7">{{ formdata.origPlanEndName }}</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="评价人"></om-label></view>
						<view class="tui-col-7">{{ formdata.evaluateUserName }}</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="调整类别" required></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.adjustTypeName }}</view>
						<view v-else class="tui-right tui-col-7">
							<picker mode="selector" @change="adjustTypeFunc" :range="adjustTypeList" :value="adjustTypeIndex" range-key="dicname">
								<view>{{ ~adjustTypeIndex && adjustTypeList.length ? adjustTypeList[adjustTypeIndex].dicname : '请选择' }}</view>
							</picker>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="拟调整完成月份" :required="editEndDateFlag"></om-label></view>
						<view v-if="checkedit || !editEndDateFlag" class="tui-right tui-col-7">{{ formdata.adjustPlanEndName }}</view>
						<view v-else class="tui-right tui-col-7">
							<picker mode="selector" @change="monthFunc" :range="monthList" :value="monthIndex" range-key="dicname">
								<view>{{ ~monthIndex && monthList.length ? monthList[monthIndex].dicname : '请选择' }}</view>
							</picker>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="变更事由类别" required></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.adjustReasonName }}</view>
						<view v-else class="tui-right tui-col-7" @click="adjustReasonFunc">
							<view>{{ formdata.adjustReasonName || '请选择' }}</view>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="备注" required></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.remark }}</view>
						<view v-else class="tui-left tui-col-7"><textarea :auto-height="true" class="om-textarea" v-model="formdata.remark" placeholder="请输入..." /></view>
					</view>
				</uni-list-item>
			</uni-list>
		</uni-card>
	</om-formsub-outer>
</template>

<script>
import { getUrl,isNotEmpty } from '@/static/js/common.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
const form = require('../../../static/js/formValidation.js');
export default {
	data() {
		return {
			checkedit: false,
			cachename: '',
			index: null,
			datas: [],
			order: 0,
			formdata: {
				id: '',
				orderNum: '', //代码
				workName: '', //工作项名称
				deliveryName: '', //交付物名称
				origPlanEndCode: '', //原计划完成月份
				origPlanEndName: '',
				evaluateUserId:'',//评价人
				evaluateUserName:'',
				evaluatePosId:'',
				evaluatePosName:'',
				adjustTypeCode: '', //调整类别
				adjustTypeName: '',
				adjustPlanEndCode: '', //拟调整完成月份
				adjustPlanEndName: '',
				adjustReasonCode: '', //变更事由类别
				adjustReasonName: '',
				planEnd: '', //原计划完成日期
				remark: '' //备注
			},
			editEndDateFlag: false, //拟调整完成日期可编辑标识
			zposId: '',
			busCode: '',
			year: '',
			editId: '',
			adjustTypeIndex: -1,
			adjustTypeList: [],
			monthIndex: -1,
			monthList: [],
			orderNumCacheKey: 'obj-work-annualworkadjustapply-orderNum-cache',
			adjustReasonCacheKey: 'obj-work-annualworkadjustapply-adjustReason-cache'
		};
	},
	watch: {
		'formdata.adjustTypeCode': {
			handler(newValue, oldValue) {
				if (newValue == '37769') {
					//改期
					this.editEndDateFlag = true;
				} else {
					this.editEndDateFlag = false;
				}
			}
		}
	},
	computed: mapState(useLoginUserStore, ['userName', 'userId']),
	methods: {
		//初始化加载选项
		initLoad() {
			let _that = this;
			uni.removeStorage({
				key: _that.cachename
			});
			this.initAdjustTypeCode(function() {
				if (_that.formdata.adjustTypeCode) {
					let res = _that.adjustTypeList;
					let flag = true;
					for (let i = 0; i < res.length; i++) {
						if (res[i].code == _that.formdata.adjustTypeCode) {
							flag = false;
							_that.adjustTypeIndex = i;
							_that.formdata.adjustTypeName = res[i].dicname;
						}
					}
					if (flag) {
						_that.om.toast('未找到符合的调整类别');
						_that.adjustTypeIndex = -1;
						_that.formdata.adjustTypeCode = '';
						_that.formdata.adjustTypeName = '';
					}
				}
			});
			this.loadMonth(function() {
				if (_that.formdata.adjustPlanEndCode) {
					let res = _that.monthList;
					let flag = true;
					for (let i = 0; i < res.length; i++) {
						if (res[i].code == _that.formdata.adjustPlanEndCode) {
							flag = false;
							_that.monthIndex = i;
							_that.formdata.adjustPlanEndName = res[i].dicname;
						}
					}
					if (flag) {
						_that.om.toast('未找到符合的拟调整完成月份');
						_that.monthIndex = -1;
						_that.formdata.adjustPlanEndCode = '';
						_that.formdata.adjustPlanEndName = '';
					}
				}
			});
		},

		//代码 点击事件
		orderNumFunc(e) {
			let arr = [];
			let rows = this.datas; //获取运营指标所有数据
			for (let i = 0; i < rows.length; i++) {
				if (i !== this.index - 1 && rows[i].orderNum) {
					arr.push(rows[i].orderNum);
				}
			}
			let url = getUrl('/pages/public/search/search', {
				cachename: this.orderNumCacheKey,
				initload: true,
				namefield: 'orderNumStr',
				notefield: 'workName',
				extrafield: 'planEnd1',
				url: 'tannualWorkAdjustApply/getOrderNumList',
				other: JSON.stringify({
					zposId: this.zposId,
					year: this.year,
					sList: arr.join(','),
					editId: this.editId
				})
			});

			uni.navigateTo({
				url: url
			});
		},

		//加载调整类别选项
		initAdjustTypeCode(callback) {
			let _that = this;
			let param = { code: '37768' }
			let code = _that.formdata.origPlanEndCode;
			if(code){
				//原计划完成月份为12月过滤改期选项
				if(code=="35815"){
					param.inCode = "37771";
				}
				_that.om.request('dictZero/getListByCode', param).then(res => {
					if (res) {
						_that.adjustTypeList = res;
					} else {
						_that.adjustTypeList = [];
					}
					if (callback) {
						callback();
					}
				});
			}else{
				_that.adjustTypeList = [];
				if (callback) {
					callback();
				}
			}	
		},

		//调整类别 点击事件
		adjustTypeFunc(e) {
			this.monthIndex = -1;
			this.monthList = [];
			this.formdata.adjustPlanEndCode = '';
			this.formdata.adjustPlanEndName = '';
			if (e) {
				if (this.adjustTypeList.length && e.target.value == -1) {
					this.adjustTypeIndex = 0;
				} else {
					this.adjustTypeIndex = e.target.value;
				}
			}
			if (~this.adjustTypeIndex && this.adjustTypeList.length) {
				this.formdata.adjustTypeCode = this.adjustTypeList[this.adjustTypeIndex].code;
				this.formdata.adjustTypeName = this.adjustTypeList[this.adjustTypeIndex].dicname;
				//加载拟调整完成月份选项
				this.loadMonth();
			} else {
				this.formdata.adjustTypeCode = '';
				this.formdata.adjustTypeName = '';
			}
		},

		/**
		 * 加载拟调整完成月份选项
		 */
		loadMonth(callback) {
			let _that = this;
			let code = _that.formdata.origPlanEndCode;
			let adjustTypecode = _that.formdata.adjustTypeCode;
			//原计划完成月份不为空 && 调整类别为改期
			if (code && adjustTypecode == '37769') {
				_that.om.request('dictZero/getListByCode', { code: '35803' }).then(res => {
					if (res) {
						//大于原计划月份
						let x = '';
						let map = {};
						let max = '';
						for (let i = 0; i < res.length; i++) {
							let obj = res[i];
							let referNum = obj.referNum;
							if (max == '') {
								max = referNum;
							} else if (referNum > max) {
								max = referNum;
							}
							map[referNum] = obj;
							if (obj.code == code) {
								x = obj.referNum;
							}
						}
						let arr = [];
						while (x < max) {
							x++;
							let obj = map[x];
							if (obj) {
								arr.push(obj);
							}
						}
						_that.monthList = arr;
					} else {
						_that.monthList = [];
					}
					if (callback) {
						callback();
					}
				});
			} else {
				_that.monthList = [];
				if (callback) {
					callback();
				}
			}
		},

		/**
		 * 拟调整完成月份
		 */
		monthFunc(e) {
			//清空明细数据
			this.dataList = [];
			if (e) {
				if (this.monthList.length && e.target.value == -1) {
					this.monthIndex = 0;
				} else {
					this.monthIndex = e.target.value;
				}
			}
			if (~this.monthIndex && this.monthList.length) {
				this.formdata.adjustPlanEndCode = this.monthList[this.monthIndex].code;
				this.formdata.adjustPlanEndName = this.monthList[this.monthIndex].dicname;
			} else {
				this.formdata.adjustPlanEndCode = '';
				this.formdata.adjustPlanEndName = '';
			}
		},

		//变更事由类别 点击事件
		adjustReasonFunc(e) {
			let url = getUrl('/pages/public/search/search', {
				cachename: this.adjustReasonCacheKey,
				initload: true,
				namefield: 'dicname',
				notefield: 'remark',
				url: 'dictZero/getListByCode',
				other: JSON.stringify({
					code: '37780'
				})
			});
			uni.navigateTo({
				url: url
			});
		},

		save() {
			let _that = this;
			let formdata = this.formdata;
			//表单规则
			let rules = [
				{
					name: 'orderNum',
					rule: ['required'],
					msg: ['请选择代码']
				},
				{
					name: 'adjustTypeCode',
					rule: ['required'],
					msg: ['请选择调整类别']
				},
				{
					name: 'adjustReasonCode',
					rule: ['required'],
					msg: ['请选择变更事由类别']
				},
				{
					name: 'remark',
					rule: ['required'],
					msg: ['请输入备注']
				}
			];

			if (this.editEndDateFlag) {
				rules.push({
					name: 'adjustPlanEndCode',
					rule: ['required'],
					msg: ['请选择拟调整完成月份']
				});
			}
			let checkRes = form.validation(formdata, rules);
			if (!checkRes) {
				if (this.index != null) {
					Object.assign(this.datas[this.index], this.formdata);
				} else {
					this.datas.push(this.formdata);
				}
				uni.setStorage({
					key: this.cachename,
					data: this.datas
				});
				uni.navigateBack();
			} else {
				_that.om.toast(checkRes);
			}
		},
		del() {
			if (this.index != null) {
				this.datas.splice(this.index, 1);
			}
			uni.setStorage({
				key: this.cachename,
				data: this.datas
			});
			uni.navigateBack();
		}
	},
	onLoad(e) {
		let index = e.index;
		let _that = this;
		if (e.checkedit) {
			if (typeof e.checkedit == 'string') {
				if (e.checkedit == 'true') {
					this.checkedit = true;
				}
			} else {
				this.checkedit = e.checkedit;
			}
		}

		if (e.cachename) {
			this.cachename = e.cachename;
			this.busCode = e.busCode;
			this.zposId = e.zposId;
			this.year = e.year;
			this.editId = e.editId;
			uni.getStorage({
				key: this.cachename,
				success: function(res) {
					if (res.data && res.data.length) {
						_that.datas = res.data;
						if (isNotEmpty(index)) {
							var d = res.data[index];
							Object.assign(_that.formdata, d);
							_that.order = parseInt(index) + 1;
							_that.index = parseInt(index);
							_that.initLoad();
						} else {
							_that.order = res.data.length + 1;
							_that.initLoad();
						}
					} else {
						_that.order = 1;
						_that.initLoad();
					}
				},
				fail() {
					_that.order = 1;
					_that.initLoad();
				}
			});
		} else {
			uni.navigateBack({});
		}
	},
	onShow() {
		let _that = this;
		uni.getStorage({
			key: _that.orderNumCacheKey,
			success: function(res) {
				let data = res.data;
				_that.formdata.orderNum = data.orderNumStr;
				_that.formdata.workName = data.workName;
				_that.formdata.deliveryName = data.deliveryName;
				_that.formdata.origPlanEndCode = data.planEndMonthCode;
				_that.formdata.origPlanEndName = data.planEndMonthName;
				_that.formdata.planEnd = data.planEnd1;
				_that.formdata.evaluateUserId = data.evaluateUserId;
				_that.formdata.evaluateUserName = data.evaluateUserName;
				_that.formdata.evaluatePosId = data.evaluatePosId;
				_that.formdata.evaluatePosName = data.evaluatePosName;
				_that.adjustTypeIndex = -1;
				_that.formdata.adjustTypeCode = '';
				_that.formdata.adjustTypeName = '';
				_that.monthIndex = -1;
				_that.monthList = [];
				_that.formdata.adjustPlanEndCode = '';
				_that.formdata.adjustPlanEndName = '';
				_that.formdata.adjustReasonCode = '';
				_that.formdata.adjustReasonName = '';
				_that.initAdjustTypeCode()
				uni.removeStorage({
					key: _that.orderNumCacheKey
				});
			}
		});

		uni.getStorage({
			key: _that.adjustReasonCacheKey,
			success: function(res) {
				let data = res.data;
				_that.formdata.adjustReasonCode = data.code;
				_that.formdata.adjustReasonName = data.dicname;
				uni.removeStorage({
					key: _that.adjustReasonCacheKey
				});
			}
		});
	}
};
</script>
<style></style>
